package com.beiing.tianshuai.tianshuai.mine;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LZQ on 2017/12/13.
 */

public class LoginRecordDao {

    LoginRecordSQLiteOpenHelper sqLiteOpenHelper;
    SQLiteDatabase db;

    public LoginRecordDao(Context context) {
        sqLiteOpenHelper = new LoginRecordSQLiteOpenHelper(context);
    }

    public void addRecords(String tel){
        db = sqLiteOpenHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("tel", tel);
        db.insert("login_record", null, values);
        db.close();
    }

    public boolean isHasRecord(String record){
        boolean isHasRecord = false;
        db = sqLiteOpenHelper.getReadableDatabase();
        Cursor cursor = db.query("login_record", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            if (record.equals(cursor.getString(cursor.getColumnIndexOrThrow("tel")))) {
                isHasRecord = true;
            }
        }
        //关闭数据库
        db.close();
        cursor.close();
        return isHasRecord;
    }

    public List<String> getRecordsList(){
        List<String> recordsList = new ArrayList<>();
        db = sqLiteOpenHelper.getReadableDatabase();
        Cursor cursor = db.query("login_record", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            recordsList.add(cursor.getString(cursor.getColumnIndexOrThrow("tel")));
        }
        //关闭数据库
        db.close();
        cursor.close();
        return recordsList;
    }

    public void deleteRecord(String delete_tel){
        db = sqLiteOpenHelper.getWritableDatabase();
        db.execSQL("delete from login_record where tel = " + delete_tel);
        db.close();
    }
}
